home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / viewt / viewt.c < prev    next >
C/C++ Source or Header  |  1995-11-05  |  6KB  |  186 lines

  1.  
  2. /* ViewT1.0 (ViewTooltypes)
  3.  *
  4.  *    by Phil Dietz
  5.  *      18-Jan-94
  6.  *
  7.  *
  8.  * DESCRIPTION: a *small* CLI program to view/edit ToolTypes for icons.
  9.  *              It's residentiable as well. PublicDomain with source included.
  10.  *
  11.  *
  12.  * GIBBERISH:   Here's a very simple program I threw together.
  13.  *      I made it cuz I was sick of having to use Workbench to view
  14.  *     or change an icons ToolTypes.  My HD controller is quite slow
  15.  *     so manipulating an icon in a dir or layers of dirs is painful.
  16.  *      Who said C couldn't write small code...[cough cough] :-)
  17.  *      
  18.  *
  19.  * USAGE:  ViewT  FILE/A,VIEW/S,ADD/K,DEL/K/N
  20.  *
  21.  *         FILE/A - Path of file icon to view (without .info extension)
  22.  *         VIEW/S - View's the tooltypes of the icon (default)
  23.  *         ADD/K  - Add's the string after ADD to the icon's tooltypes
  24.  *         DEL/K/N- Delete's the number of the ToolType line shown by VIEW.
  25.  *
  26.  *  ie  show tooltypes:  ViewT sys:wbstartup/SetDefMon
  27.  *                   or  ViewT sys:wbstartup/SetDefMon view
  28.  *
  29.  *          add a type:  ViewT sys:wbstartup/SetDefMon add "FOOD=BIG TACO"
  30.  *
  31.  *      delete entry 5:  ViewT sys:wbstartup/SetDefMon del 5
  32.  *
  33.  *
  34.  * REQUIREMENTS:  WB2.04 or greater
  35.  *
  36.  *
  37.  * COMPILE:  compile: 'sc ViewT nolink'  (use SCOPTIONS provided)
  38.  *           link   : 'slink viewt.o to ViewT  ND SC SD'
  39.  *
  40.  * Set tab size to 8.
  41.  * I didn't clean the code so don't jump on me :-)
  42.  * For small link utilites (w/ src) check out Newlist8.2 found on Aminet.
  43.  */
  44.  
  45.  
  46.  
  47. #include <stdio.h>
  48.  
  49. #include <proto/exec.h>
  50. #include <proto/dos.h>
  51. #include <proto/icon.h>
  52.  
  53. #include <exec/execbase.h>
  54. #include <exec/memory.h>
  55. #include <libraries/dos.h>
  56. #include <libraries/dosextens.h>
  57. #include <devices/conunit.h>
  58. #include <workbench/workbench.h>
  59.  
  60. /* Defines */
  61. #define TEMPLATE   "FILE/A,VIEW/S,ADD/K,DEL/K/N"
  62. #define NUM        4
  63.  
  64. #define ARG_NAME    args[0]
  65. #define ARG_VIEW    args[1]
  66. #define ARG_ADD        args[2]
  67. #define ARG_DEL        args[3]
  68.  
  69. /* Version String */
  70. static char ver[]="$VER: ViewT1.0 (18-Jan-94)";
  71.  
  72. /* Protos */
  73. void PRintf(struct DOSBase *, char *, long , ...);
  74.  
  75. void __saveds mymain(void)
  76. {
  77.  
  78.     struct ExecBase        *SysBase = (*((struct ExecBase **) 4));
  79.     struct IconBase        *IconBase;
  80.     struct DOSBase        *DOSBase;
  81.     struct WBStartup    *wbMsg = NULL;
  82.     struct Process        *process;
  83.     struct RDArgs        *rd;
  84.     struct DiskObject    *dob=NULL;
  85.  
  86.     LONG    args[4];    /* ARGS template memory        */
  87.     int    rc=1;        /* return code..assume error    */
  88.     int    i,j;        /* general counter crap        */
  89.  
  90.     char     **table;
  91.     int    count=0;
  92.  
  93.     process = (struct Process *) SysBase->ThisTask;
  94.     if (!(process->pr_CLI)) {            /* WB-launched code */
  95.         WaitPort (&process->pr_MsgPort);
  96.         wbMsg = (struct WBStartup *) GetMsg (&process->pr_MsgPort);
  97.     }
  98.  
  99.     if (SysBase->LibNode.lib_Version < 37) goto xit;
  100.  
  101.     DOSBase = (struct DOSBase  *)OpenLibrary ("dos.library", 37);
  102.     IconBase= (struct IconBase *)OpenLibrary ("icon.library",37);
  103.  
  104.     if(DOSBase) {
  105.         if(IconBase) {
  106.             rd = ReadArgs(TEMPLATE, &args, NULL);    /* Parse args */
  107.  
  108.             if(rd) {
  109.             
  110.                 if (ARG_NAME) {
  111.                     dob = GetDiskObjectNew((STRPTR)ARG_NAME);
  112.                     if(dob && dob->do_ToolTypes) {
  113.  
  114.     /* To add an entry we must */        while(dob->do_ToolTypes[count] && *dob->do_ToolTypes[count]) count++;    /* get number of tooltypes */
  115.     /* make a completely new   */
  116.     /* char. table (though we  */        if(ARG_ADD) {
  117.     /* can copy most pointers) */            if(FindToolType(dob->do_ToolTypes,(STRPTR)ARG_ADD)) {
  118.     /* Once we do that we      */                PutStr("ToolType already exists.\n");
  119.     /* attach the new table    */                goto klose;
  120.     /* to the old structure    */            }
  121.     /* then save.           */            table=(char **)AllocVec(sizeof(char *)*(count+2), MEMF_PUBLIC | MEMF_CLEAR);
  122.     /* NOTE: it should be safe */            if(table) {
  123.     /* since deallocation of   */                for(i=0;i<count;i++) table[i]=dob->do_ToolTypes[i];
  124.     /* old table is done from  */                table[count]    =(char *)ARG_ADD;
  125.     /* an internal freelist.   */                table[count+1]    =NULL;
  126.  
  127.                                 dob->do_ToolTypes=table;
  128.                                 PutDiskObject((STRPTR)ARG_NAME,dob);
  129.                                 FreeVec(table);
  130.                             }
  131.                         }
  132.     /* Delete we just shift  */        else if(ARG_DEL) {
  133.     /* the pointer list over */            j=*(LONG *)ARG_DEL;  /* put real number in j */
  134.     /* the entry (aka write  */            if(j<1 || j>count) {
  135.     /* over).                */                PutStr("Invalid ToolType number.\n");
  136.                                 goto klose;
  137.                             }
  138.                             i=j-1;    /* position on entry to delete/overwrite */
  139.                             if(dob->do_ToolTypes[i]) {
  140.                                 while(dob->do_ToolTypes[i+1]) {
  141.                                     dob->do_ToolTypes[i]=dob->do_ToolTypes[i+1];
  142.                                     i++;
  143.                                 }
  144.                             }
  145.                             dob->do_ToolTypes[i]=NULL;
  146.                             PutDiskObject((STRPTR)ARG_NAME,dob);
  147.                         }
  148.                         else {  /* else VIEW */
  149.                             i=0;
  150.                             while(dob->do_ToolTypes[i] && *dob->do_ToolTypes[i]) {
  151.                                 PRintf(DOSBase,"%ld. %s\n",i+1,(char *)dob->do_ToolTypes[i]);
  152.                                 i++;    /* increment pointer pointer    */
  153.                             }
  154.                             if(i==0) PutStr("No tooltypes.\n");
  155.                         }
  156.                     }
  157.                     else PRintf(DOSBase,"File '%s.info' missing.\n",(char *)ARG_NAME);
  158.                 }
  159.                 else PutStr("No file given.\n");
  160.             }
  161.             else PutStr("Missing argument.\n");
  162.  
  163. klose:            if(dob)   FreeDiskObject(dob);    /* Free disk object        */
  164.             if(rd)    FreeArgs(rd);        /* Free the readargs structure  */
  165.             rc=0;                /* return good return code    */
  166.  
  167.             CloseLibrary((struct Library *)IconBase);
  168.         }
  169.         CloseLibrary((struct Library *)DOSBase);
  170.     }
  171.  
  172. xit:    if (wbMsg) {
  173.         Forbid ();                /* Very important! */
  174.         ReplyMsg ((struct Message *) wbMsg);    /* Must reply Workbench Msg */
  175.     }
  176.     process->pr_Result2=rc;
  177. }
  178.  
  179. void PRintf(struct DOSBase *DOSBase, char *string, long arg, ...)
  180.       {
  181.       /* We're passing DOSBase cuz there are no globals in a RESIDENTIABLE
  182.        * with NO cres.o startup code.
  183.        */  
  184.       VPrintf(string, &arg);
  185.       }
  186.